<template>
  <el-dialog
    :visible="visible"
    :before-close="closesDialog"
    width="30%"
    title="添加活动附加分">

    <el-form ref="scoreDataForm" :model="formdata" label-position="left" label-width="80px">
      <el-form-item
        :rules="{ required: true, message: '请填写学生学号', trigger: 'blur' }"
        prop="userid"
        label="学生学号">
        <el-input :disabled="true" v-model="formdata.userid"/>
      </el-form-item>
      <el-form-item
        :rules="{ required: true, message: '请填写附加分数', trigger: 'blur' }"
        prop="acaddscore"
        label="附加分数">
        <el-input-number :min="0.01" :precision="2" :step="0.05" :max="10" v-model="formdata.acaddscore"/>
      </el-form-item>
      <el-form-item
        :rules="{ required: true, message: '请填写附加分数备注', trigger: 'blur' }"
        prop="addscoreexplain"
        label="备注">
        <el-input v-model="formdata.addscoreexplain"/>
      </el-form-item>
    </el-form>

    <span slot="footer" class="dialog-footer">
      <el-button @click="closesDialog">取 消</el-button>
      <el-button type="primary" @click="handleAddScore">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addScore } from '@/api/activity'

export default {
  name: 'AddscoreDialog',
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    userId: {
      type: String,
      default: '',
      required: false
    },
    acId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      formdata: {}
    }
  },
  watch: {
    userId: function(n) {
      this.formdata.userid = n
    }
  },
  methods: {
    handleAddScore() {
      this.$refs.scoreDataForm.validate(valid => {
        if (!valid) {
          return
        }
        addScore(this.cleanData(this.formdata))
          .then(res => {
            this.$message(res.msg)
          })
          .catch(() => {})
          .finally(() => {
            this.$emit('update')
            this.closesDialog()
          })
      })
    },
    cleanData(data) {
      const res = {}
      res.AcId = this.acId
      res.userid = this.userId
      res.acaddscore = this.formdata.acaddscore.toString()
      res.addscoreexplain = this.formdata.addscoreexplain
      return res
    },
    closesDialog() {
      this.$emit('close')
    }
  }
}
</script>

<style scoped>

</style>
